package com.controller;

import com.pojo.Department;
import com.pojo.Role;
import com.service.DepartmentService;
import com.service.RoleService;
import com.vo.Result;
import com.vo.roleTreeVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/sys")
public class Systems {
    @Resource
    private RoleService roleService;
    @Resource
    private DepartmentService departmentService;

    @RequestMapping("/role")
    public String role(){
        return "System/roleandpermission";
    }


    @RequestMapping("/departmentall")
    @ResponseBody
    public Result departmentall(){
        List<Department> selectdepartall = departmentService.selectdepartall();
        return Result.success(selectdepartall);
    }

    @RequestMapping("/findall")
    @ResponseBody
    public Result findallrole(){
        List<Role> allRole = roleService.findAllRole();
        return Result.success(allRole);
    }

    @RequestMapping("role/query/{roleId}")
    @ResponseBody
    public Result queryPermission(@PathVariable Integer roleId){
        List<roleTreeVo> treeVoList = roleService.queryTreeData(roleId,null);
        return Result.success(treeVoList);
    }

    @RequestMapping("role/grant/{roleId}/{permIds}")
    @ResponseBody
    public Result grantPermission(@PathVariable Integer roleId,@PathVariable Integer[] permIds){
        try {
            roleService.roleGrant(roleId,permIds);
            return Result.success("授权成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.success("授权失败");
        }
    }

}
